package com.example.eldersystem.operatedata.service;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.mapper.OldPersonMapper;
import com.example.eldersystem.operatedata.model.OldPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OldPersonService {

    private OldPersonMapper oldPersonMapper;

    @Autowired
    public void setOldPersonMapper (OldPersonMapper oldPersonMapper) {
        this.oldPersonMapper = oldPersonMapper;
    }

    public int getOldPersonNum(){
        return oldPersonMapper.selectOldPersonNum();
    }

    public DataResponse<String> addOldPerson(OldPerson oldPerson){
        DataResponse<String> dataResponse = new DataResponse<>();
        if (oldPersonMapper.addOldPerson(oldPerson)>0){
            dataResponse.setMsg("添加老人成功");
        }else {
            dataResponse.setCode(81);
            dataResponse.setMsg("添加老人失败");
        }
        return dataResponse;
    }


    public DataResponse<List<OldPerson>> selectOldPerson(int from, int num) {
        DataResponse<List<OldPerson>> dataResponse = new DataResponse<>();
        List<OldPerson> list;
        int total = oldPersonMapper.selectOldPersonNum();
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = oldPersonMapper.selectOldPerson(from,num);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<List<OldPerson>> selectOldPersonByName(String userName) {
        DataResponse<List<OldPerson>> dataResponse = new DataResponse<>();
        List<OldPerson> list;
        int total = oldPersonMapper.selectOldPersonNum2(userName);
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = oldPersonMapper.selectOldPersonByName(userName);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<String> updateOldPerson(OldPerson oldPerson){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = oldPersonMapper.updateOldPerson(oldPerson);
        if(num==0){
            dataResponse.setCode(101);
            dataResponse.setMsg("更新失败，查询不到对应的老人");
        }else{
            dataResponse.setMsg("更新成功");
        }
        return dataResponse;
    }

    public DataResponse<String> updateProfile(int id,String profilePhoto){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = oldPersonMapper.updateProfile(id,profilePhoto);
        dataResponse.setCode(0);
        if(num==0){
            dataResponse.setCode(101);
            dataResponse.setMsg("更新失败，查询不到对应的老人");
        }else{
            dataResponse.setMsg("更新照片成功");
        }
        return dataResponse;
    }

    public DataResponse<String> deleteOldPerson(int id){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = oldPersonMapper.deleteOldPerson(id);
        if(num==0){
            dataResponse.setCode(91);
            dataResponse.setMsg("删除失败");
        }else{
            dataResponse.setMsg("删除成功");
        }
        return dataResponse;
    }



}
